<?php

namespace App\Http\Controllers\Innerend;

use App\Manage\AccountManage;
use App\Models\SiteWechat;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Crypt;
use App\Models\Admin;
use Illuminate\Support\Facades\Redirect;

class AccountController extends BaseController
{
    // 个人账户

    // 初始化
    public function initialize()
    {
        parent::initialize();
    }

    // 修改
    public function edit()
    {
        $myUser = $this->myUser;
        if(empty($myUser)){
            $this->jumpLogin('请登录后在操作！！');
        }

        $formHtml = '';
        $form     = $this->adminForm();

        $username = $form->formInput('username', $myUser['username'], '用户名');
        $nickname = $form->formInput('nickname', $myUser['nickname'], '昵称');
        $mobile   = $form->formInput('mobile', $myUser['mobile'], '手机号');

        $formHtml .= $form->makeItem('用户名', $username);
        $formHtml .= $form->makeItem('昵称', $nickname);
        $formHtml .= $form->makeItem('手机号', $mobile);


        return $this->dispaly('form', ['formHtml' => $formHtml]);
    }

    // 修改密码
    public function password()
    {
        $myUser = $this->myUser;
        if(empty($myUser)){
            $this->jumpLogin('请登录后在操作！！');
        }


        $formHtml = '';
        $form     = $this->adminForm();

        $password  = $form->formInput('password', '', '密码');
        $password2 = $form->formInput('password2', '', '确认密码');

        $formHtml .= $form->makeItem('密码', $password);
        $formHtml .= $form->makeItem('确认密码', $password2);


        return $this->dispaly('form', ['formHtml' => $formHtml]);
    }


    // 修改
    public function onEdit()
    {
        $this->throwIf(empty($this->myUser), '请登录后在操作！！', LOGIN_ERROR);

        $data = [
            //'username' => $this->getData('username', null, 'trim'),
            //'role'     => $this->getData('role', null, 'trim'),
            'nickname' => $this->getData('nickname', null, 'trim'),
            'password' => $this->getData('password', null, 'trim'),
            'mobile'   => $this->getData('mobile', null, 'trim'),
            //'status'   => $this->getData('status', 1, 'intval),
        ];

        // 检查唯一性
        $model = new Admin();
        $data  = $this->filterNull($data);
        $data  = $model->checkData($data);
        $this->throwIf($data === false, $model->getError());

        // 验证
        $result = $model->checkRules($data, 'info');
        $this->throwIf($result === false, $model->getError());

        $where  = ['id' => $this->myUser['id']];
        $result = $model->oneEdit($where, $data);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

    // 修改密码
    public function onPassword()
    {
        $this->throwIf(empty($this->myUser), '请登录后在操作！！', LOGIN_ERROR);

        $data = ['password' => $this->getData('password', null, 'trim'),];
        // 检查唯一性
        $model = new Admin();
        $data  = $this->filterNull($data);
        $data  = $model->checkData($data);
        $this->throwIf($data === false, $model->getError());

        $where  = ['id' => $this->myUser['id']];
        $result = $model->oneEdit($where, $data);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }


    // 登录
    public function login()
    {
        return $this->dispaly('account.login', []);
    }


    // 退出
    public function logout()
    {
        //Cookie::queue(Cookie::make('innerend_user', json_encode([])));
        //return $this->jumpLogin();

        // 删除cookie后需要重新定向
        $cookie = Cookie::forget('innerend_user');
        $url    = Url('innerend/account/login');

        return Redirect::secure($url)->withCookie($cookie);
    }


    // 登录
    public function onLogin()
    {
        $admin = new AccountManage();
        $user  = $admin->onLogin();
        if($user === false){
            return $this->error($admin->getError());
        }

        // 保存session
        $this->optLogic()->request()->session()->put('innerend_user', $user);
        Cookie::queue(Cookie::make('innerend_user', json_encode($user)));
        //Cookie::queue('user', json_encode($user));
        //cookie('user', json_encode($user));

        return $this->success($user, '登录成功！');
    }

}


